﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.LabManage
{
    public partial class PreViewLocation : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("SYYF");
            if (UserType == "student" || UserType == "teacher")
            {
                modifybtn.Visible = false;
                DeleteBtn.Visible = false;
            }
            if (Request.QueryString["ID"] != null)
            {
                if (!IsPostBack)
                {
                    int ID;
                    string strID = Request.QueryString["ID"].ToString();
                    if (int.TryParse(strID, out ID))
                    {

                        LabMS.BLL.Location location = new LabMS.BLL.Location();
                        LabMS.Model.Location Locationinfo = new LabMS.Model.Location();
                        List<LabMS.Model.Dictionary> list = new List<LabMS.Model.Dictionary>();

                        IDHF.Value = ID.ToString();//存储ID值
                        //获取用房信息 通过ID
                        Locationinfo = location.GetModel(ID);

                        LocationNameLbl.Text = Locationinfo.Location_Name;//名称
                        AreaLbl.Text = Locationinfo.Location_Area;//面积
                        BuildingNameLbl.Text = Locationinfo.Location_Building;//楼名称
                        CampusLbl.Text = Locationinfo.Location_Campus;//校区
                        FloorLbl.Text = Locationinfo.Location_Floor;//楼层
                        ManagerLbl.Text = Locationinfo.Location_InCharge;//负责人
                        BuildingNameLbl.Text = Locationinfo.Location_Name;//楼名称
                        TelLbl.Text = Locationinfo.Location_Phone;//联系电话
                        RoomNumLbl.Text = Locationinfo.Location_RoomNum;//房间号
                        SummaryLbl.Text = Locationinfo.Location_Summary.Replace("\r\n", "<br/>");//备注
                        UsageLbl.Text = Locationinfo.Location_Usage;//用途
                        StudentNumLbl.Text = Locationinfo.Location_StudentNum.ToString();//容纳学生数

                        //绑定使用状态
                        list = DictionaryDropdownlist("117");
                        StatusDDL.DataSource = list;
                        StatusDDL.DataBind();

                        //绑定实验室名
                        LabDropdowlist();
                        LabDDL.SelectedValue = Locationinfo.LabID.ToString();
                    }
                    else
                    {
                        ERRLbl.Text = "系统异常，ID值不合法！";
                        ERRLbl.Visible = true;
                        modifybtn.Visible = false;
                        DeleteBtn.Visible = false;
                    }
                }
            }
            else
            {
                ERRLbl.Text = "系统异常，ID值不合法！";
                ERRLbl.Visible = true;
                modifybtn.Visible = false;
                DeleteBtn.Visible = false;
            }
        }

        //绑定数据字典 返回List
        protected List<LabMS.Model.Dictionary> DictionaryDropdownlist(string code)
        {
            string strwhere = "DClass='" + code + "'";
            LabMS.BLL.Dictionary dic = new LabMS.BLL.Dictionary();

            List<LabMS.Model.Dictionary> list = new List<LabMS.Model.Dictionary>();
            list = dic.GetModelList(strwhere);
            return list;
        }

        //绑定所属实验室
        protected void LabDropdowlist()
        {
            LabMS.BLL.Lab lab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> ParLab = new List<LabMS.Model.Lab>();
            List<LabMS.Model.Lab> SonLab = new List<LabMS.Model.Lab>();
            List<LabMS.Model.Lab> LabTemp = new List<LabMS.Model.Lab>();

            ParLab = lab.GetModelList("Lab_ParentID is null");
            SonLab = lab.GetModelList("Lab_ParentID is not null");
            LabTemp = lab.GetModelList("Lab_ParentID is null");

            int count = ParLab.Count, count1 = SonLab.Count;
            for (int i = 0; i < count; i++)
            {
                int ID = ParLab[i].ID;
                int m = 0;
                for (int j = 0; j < count1; j++)
                {
                    LabMS.Model.Lab labModel = new LabMS.Model.Lab();
                    int ParentID = int.Parse(SonLab[j].Lab_ParentID.ToString());

                    if (ParentID == ID)
                    {
                        labModel.ID = SonLab[j].ID;
                        labModel.Lab_Name = "|-" + SonLab[j].Lab_Name;
                        LabTemp.Insert(i + m + 1, labModel);
                        m++;
                    }
                }
            }
            LabDDL.DataSource = LabTemp;
            LabDDL.DataBind();
            ListItem item = new ListItem("无", "0");
            LabDDL.Items.Insert(0, item);
        }

        //删除实验用房信息
        protected void Delete_OnClick(object sender, EventArgs e)
        {
            ExtendBLL.Location loc = new ExtendBLL.Location();
            try
                {
                    int ID = int.Parse(IDHF.Value);
                    List<string> strID = new List<string>();
                    List<string> strTable = new List<string>();
                    List<string> strColName = new List<string>();
                    #region 用房
                    strID.Add(ID.ToString());
                    strTable.Add("Location");
                    strColName.Add("ID");
                    #endregion
                    #region 课程安排
                    strID.Add(ID.ToString());
                    strTable.Add("Arrangement");
                    strColName.Add("LocationID");
                    #endregion
               
                    loc.DeleteLocation(strID, strTable, strColName);
                    Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script type='text/javascript'>alert('删除成功!');refreshParent();wincolse();</script>");
                }
                catch
                {
                    Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script type='text/javascript'>alert('删除失败!');refreshParent();wincolse();</script>");
                    return;
                }
        }
    }
}
